package day_03;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientChat extends JFrame {
    public static void main(String[] args) {
        new ClientChat();
    }
    private JTextArea jta;//文本area
    private JScrollPane jsp;//滚动条
    private JPanel jp;//面板
    private JTextField jtf;//文本框
    private JButton jb;//按钮
    private Socket socket = null;

    public JTextArea getJta() {
        return jta;
    }

    public void setJta(JTextArea jta) {
        this.jta = jta;
    }

    public JScrollPane getJsp() {
        return jsp;
    }

    public void setJsp(JScrollPane jsp) {
        this.jsp = jsp;
    }

    public JPanel getJp() {
        return jp;
    }

    public void setJp(JPanel jp) {
        this.jp = jp;
    }

    public JTextField getJtf() {
        return jtf;
    }

    public void setJtf(JTextField jtf) {
        this.jtf = jtf;
    }

    public JButton getJb() {
        return jb;
    }

    public void setJb(JButton jb) {
        this.jb = jb;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ClientChat(){
        //构建我们的聊天窗口
        jta = new JTextArea();
        jta.setEditable(false);
        jsp = new JScrollPane(jta);
        jp = new JPanel();
        jtf = new JTextField(20);
        jb = new JButton("发送");
        jp.add(jtf);
        jp.add(jb);

        //布局策略
        this.add(jsp, BorderLayout.CENTER);
        this.add(jp, BorderLayout.SOUTH);

        this.setTitle("QQ 客户端");
        this.setSize(500,500);
        this.setLocation(100,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("按钮被点击");
                //获得文本数据
                sendMsg();

            }
        });
        //监听回车事件
        jtf.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }
            @Override
            public void keyPressed(KeyEvent e) {
//                System.out.println(e);
                if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    //发送
                    sendMsg();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        //client start

        //创建Socket，指定ip，port

        try {
            socket = new Socket("127.0.0.1", 8080);
            //获得基于Socket的输入流和输出流
//            PrintWriter os = new PrintWriter(socket.getOutputStream());//发给服务器的数据
            //独立的接收服务端数据的线程
            ClientThread ct = new ClientThread(socket,this);
            ct.start();//接收服务器的信息的线程
            //向服务器写数据
            //获得键盘输入
//            BufferedReader sin = new BufferedReader(new InputStreamReader(
//                    System.in));
//            String readline;
//            while (!(readline=sin.readLine()).equals("exit")) {
//                os.println(readline);
//                os.flush();//发给了服务器
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //client end
    }
    //发送文本区数据
    public void sendMsg(){
        String text = jtf.getText();
//                jta.append(text);
        //发送数据
//
        BufferedWriter bw;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(text);
            bw.newLine();//换行发送
            bw.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}